package com.jingtai.platform.service.shop.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jingtai.common.enums.DeleteFlagEnum;
import com.jingtai.common.template.ResultMapInfo;
import com.jingtai.core.entity.shop.Evaluation;
import com.jingtai.core.mapper.shop.EvaluationMapper;
import com.jingtai.platform.service.shop.EvaluationService;
import com.jingtai.platform.util.ResultUtil;
import com.jingtai.platform.util.UrlToMap;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Map;


/**
 * 评价表 impl
 *
 * @author sunlaiqian
 * @date 2020-12-02 18:31:33
 */
@Service
public class EvaluationServiceImpl extends ServiceImpl<EvaluationMapper, Evaluation> implements EvaluationService {

    @Autowired
    private EvaluationService evaluationService;

    @Override
    public ResultUtil Evaluationlist(Integer page, Integer limit, String conditions){
        QueryWrapper<Evaluation> wrapper = new QueryWrapper<>();
        Map<String, String> map = UrlToMap.paramToMap(conditions);
        for(Map.Entry<String, String> entry : map.entrySet()){
            wrapper.like(entry.getKey(), entry.getValue());
        }
        wrapper.eq("delete_flag", DeleteFlagEnum.NOT_DELETE.getCode());
        return new ResultUtil(ResultMapInfo.GETSUCCESS,evaluationService.page(new Page<>(page,limit),wrapper));
    }

    @Override
    public ResultUtil addEvaluation(Evaluation evaluation) {
        boolean save = evaluationService.save(evaluation);
        return new ResultUtil(save ? ResultMapInfo.ADDSUCCESS:ResultMapInfo.ADDFAIL);
    }

    @Override
    public ResultUtil getEvaluationById(Long id) {
        Evaluation evaluation = evaluationService.getById(id);
        return evaluation == null ?  new ResultUtil(ResultMapInfo.GETFAIL) : new ResultUtil(ResultMapInfo.GETSUCCESS,evaluation);
    }

    @Override
    public ResultUtil delEvaluationById(Long id) {
        Evaluation evaluation = new Evaluation();
        evaluation.setId(id);
        evaluation.setDeleteFlag(DeleteFlagEnum.DELETE.getCode());
        boolean delState = evaluationService.updateById(evaluation);
        return delState ?  new ResultUtil(ResultMapInfo.DELETESUCCESS) : new ResultUtil(ResultMapInfo.DELETEFAIL);
    }

    @Override
    public ResultUtil editEvaluation(Evaluation evaluation) {
        boolean editState = evaluationService.updateById(evaluation);
        return editState ?  new ResultUtil(ResultMapInfo.EDITSUCCESS) : new ResultUtil(ResultMapInfo.EDITFAIL);
    }

}
